//
//  OptionLayer.cpp
//  Clicker Heroes
//
//  Created by Cloner Team on 11/2/14.
//
//

#include "OptionLayer.h"
OptionLayer::OptionLayer() {
    this->gameModel = GameModel::getInstance();
    this->soundModel = SoundModel::getInstance();
    this->initialize();
}

OptionLayer::~OptionLayer() {
    
}

void OptionLayer::onEnter() {
    Layer::onEnter();
    this->touchesRegister();
}

void OptionLayer::touchesRegister() {
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan = CC_CALLBACK_2(OptionLayer::onTouchesBegan, this);
    listener->onTouchesMoved = CC_CALLBACK_2(OptionLayer::onTouchesMoved, this);
    listener->onTouchesEnded = CC_CALLBACK_2(OptionLayer::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void OptionLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
{
    for (auto touch: touches){
        Point point = this->convertTouchToNodeSpace(touch);
        Point optionBoardPoint = point - Point(this->optionBoard->getPositionX() - this->optionBoard->getContentSize().width/2, this->optionBoard->getPositionY() - this->optionBoard->getContentSize().height/2);
        Point downSfxBarPoint = optionBoardPoint - Point(this->downSfxBar->getPositionX() , this->downSfxBar->getPositionY() - this->downSfxBar->getContentSize().height/2);
        Point downBgmBarPoint = optionBoardPoint - Point(this->downBgmBar->getPositionX() , this->downBgmBar->getPositionY() - this->downBgmBar->getContentSize().height/2);
        //sfx control
        RectBody* sfxControlRect = new RectBody(this->sfxControl->getContentSize().width, this->sfxControl->getContentSize().height);
        sfxControlRect->setPosition(Point(this->sfxControl->getPositionX(), this->sfxControl->getPositionY()));
        if(Utils::checkPointInRect(sfxControlRect, downSfxBarPoint)) {
            this->isControlSfx = true;
        }
        
        //bgm control
        RectBody* bgmControlRect = new RectBody(this->bgmControl->getContentSize().width, this->bgmControl->getContentSize().height);
        bgmControlRect->setPosition(Point(this->bgmControl->getPositionX(), this->bgmControl->getPositionY()));
        if(Utils::checkPointInRect(bgmControlRect, downBgmBarPoint)) {
            this->isControlBgm = true;
        }
    }
}

void OptionLayer::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
    for (auto touch: touches){
        Point point = this->convertTouchToNodeSpace(touch);
        Point optionBoardPoint = point - Point(this->optionBoard->getPositionX() - this->optionBoard->getContentSize().width/2, this->optionBoard->getPositionY() - this->optionBoard->getContentSize().height/2);
        Point downSfxBarPoint = optionBoardPoint - Point(this->downSfxBar->getPositionX() , this->downSfxBar->getPositionY() - this->downSfxBar->getContentSize().height/2);
        Point downBgmBarPoint = optionBoardPoint - Point(this->downBgmBar->getPositionX() , this->downBgmBar->getPositionY() - this->downBgmBar->getContentSize().height/2);
        //sfx control
        if(this->isControlSfx == true) {
            this->sfxControl->setPositionX(downSfxBarPoint.x);
            if(this->sfxControl->getPositionX() - this->sfxControl->getContentSize().width / 2 < 0) {
                this->sfxControl->setPositionX(this->sfxControl->getContentSize().width / 2);
            }
            if(this->sfxControl->getPositionX() + this->sfxControl->getContentSize().width / 2 > this->downSfxBar->getContentSize().width){
                this->sfxControl->setPositionX(this->downSfxBar->getContentSize().width - this->sfxControl->getContentSize().width / 2);
            }
        }
        //bgm control
        if(this->isControlBgm == true) {
            this->bgmControl->setPositionX(downBgmBarPoint.x);
            if(this->bgmControl->getPositionX() - this->bgmControl->getContentSize().width / 2 < 0) {
                this->bgmControl->setPositionX(this->bgmControl->getContentSize().width / 2);
            }
            if(this->bgmControl->getPositionX() + this->bgmControl->getContentSize().width / 2 > this->downBgmBar->getContentSize().width){
                this->bgmControl->setPositionX(this->downBgmBar->getContentSize().width - this->bgmControl->getContentSize().width / 2);
            }
        }

    }
}

void OptionLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
    this->isControlBgm = false;
    this->isControlSfx = false;
}

void OptionLayer::onExit() {
    Layer::onExit();
    this->removeAllChildrenWithCleanup(true);
}

void OptionLayer::update(float dt) {
    //update base
    if(this->gameModel->getGameState() == GS_OPTION){
        this->grayLayer->setVisible(true);
        this->mainLayer->setVisible(true);
    } else {
        this->grayLayer->setVisible(false);
        this->mainLayer->setVisible(false);
    }
    //percentage of bgm bar and sfx bar
    this->bgmBar->setPercentage(100 * (this->bgmControl->getPositionX() - this->bgmControl->getContentSize().width/2) /  this->downBgmBar->getContentSize().width);
    this->sfxBar->setPercentage(100 * (this->sfxControl->getPositionX() - this->sfxControl->getContentSize().width/2) /  this->downSfxBar->getContentSize().width);
    //update sound volume
    this->soundModel->setBackgroundMusicVolume(this->bgmBar->getPercentage() / 100);
    this->soundModel->setEffectsVolume(this->sfxBar->getPercentage() / 100);
}

void OptionLayer::initialize(){
    this->mainLayer = 0;
    this->isControlBgm = false;
    this->isControlSfx = false;
    this->createLayers();
    this->createLayersContent();
}

void OptionLayer::createLayers()
{
    this->grayLayer = UGLayerColor::create();
    this->grayLayer->initWithColor(Color4B(0,0,0,100));
    this->addChild(this->grayLayer);
    
    this->mainLayer = UGLayer::create();
    this->addChild(this->mainLayer);
}
void OptionLayer::createLayersContent(){
    Size gameSize = this->gameModel->getDisplayResolutionSize();
    //optionBoard
    this->optionBoard = new Face();
    this->optionBoard->initWithSpriteFrameName(ANI_OPTION_BOARD);
    this->optionBoard->setPosition(gameSize.width/2, gameSize.height/2);
    this->mainLayer->addChild(this->optionBoard);
    Size optionBoardSize = this->optionBoard->getContentSize();
    
    //close button
    UGMenu* closeMenu = UGMenu::create();
    closeMenu->setPosition(Point::ZERO);
    this->optionBoard->addChild(closeMenu);
    
    MenuItemSprite* closeButton = Utils::createButton((char*)ANI_BUTTON_CLOSE_1, (char*)ANI_BUTTON_CLOSE_2);
    closeButton->setPosition(this->optionBoard->getContentSize().width + closeButton->getContentSize().width/2 - 50, this->optionBoard->getContentSize().height - closeButton->getContentSize().height/2);
    closeMenu->addChild(closeButton);
    closeButton->setCallback(CC_CALLBACK_1(OptionLayer::onCloseOptionBoard, this));

    //sfx bar
    {
        this->downSfxBar = new Face();
        this->downSfxBar->initWithSpriteFrameName(ANI_VOLUME_BAR_DOWN);
        this->downSfxBar->setAnchorPoint(Point(0,0.5));
        this->downSfxBar->setPosition(Point(152,310));
        this->optionBoard->addChild(this->downSfxBar);
        
        Face* progressFace = new Face();
        progressFace->initWithSpriteFrameName(ANI_VOLUME_BAR_UP);
        this->sfxBar = ProgressTimer::create(progressFace);
        this->sfxBar->setType(ProgressTimer::Type::BAR);
        this->sfxBar->setMidpoint(Point(0,0));
        this->sfxBar->setBarChangeRate(Point(1,0));
        this->sfxBar->setPercentage(50);
        this->sfxBar->setPosition(downSfxBar->getContentSize().width/2, downSfxBar->getContentSize().height/2);
        this->downSfxBar->addChild(this->sfxBar);
        
        this->sfxControl = new Face();
        this->sfxControl->initWithSpriteFrameName(ANI_VOLUME_CONTROL);
        this->sfxControl->setPosition(downSfxBar->getContentSize().width/2, downSfxBar->getContentSize().height/2);
        this->downSfxBar->addChild(this->sfxControl);
    }
    
    //bgm bar
    {
        this->downBgmBar = new Face();
        this->downBgmBar->initWithSpriteFrameName(ANI_VOLUME_BAR_DOWN);
        this->downBgmBar->setAnchorPoint(Point(0,0.5));
        this->downBgmBar->setPosition(Point(152,254));
        this->optionBoard->addChild(this->downBgmBar);
        
        Face* progressFace = new Face();
        progressFace->initWithSpriteFrameName(ANI_VOLUME_BAR_UP);
        this->bgmBar = ProgressTimer::create(progressFace);
        this->bgmBar->setType(ProgressTimer::Type::BAR);
        this->bgmBar->setMidpoint(Point(0,0));
        this->bgmBar->setBarChangeRate(Point(1,0));
        this->bgmBar->setPercentage(100);
        this->bgmBar->setPosition(downBgmBar->getContentSize().width/2,downBgmBar->getContentSize().height/2);
        this->downBgmBar->addChild(this->bgmBar);
        
        this->bgmControl = new Face();
        this->bgmControl->initWithSpriteFrameName(ANI_VOLUME_CONTROL);
        this->bgmControl->setPosition(downBgmBar->getContentSize().width / 2, downBgmBar->getContentSize().height / 2);
        this->downBgmBar->addChild(this->bgmControl);
    }
    
    //Animation Label
    Label* animationLabel = Utils::createLabel((char*)"Animation", (char*)FONT_TRANSFORMER, 30);
    animationLabel->setPosition(112, 185);
    animationLabel->setColor(Color3B(128, 215, 255));
    this->optionBoard->addChild(animationLabel);
    
    //offAnimation Face
    Face* offAnimationFace = new Face();
    offAnimationFace->initWithSpriteFrameName((char*)ANI_BUTTON_OFF);
    offAnimationFace->setPosition(323, 185);
    this->optionBoard->addChild(offAnimationFace);
    
    //reset button
    UGMenu* resetMenu = UGMenu::create();
    resetMenu->setPosition(Point::ZERO);
    this->optionBoard->addChild(resetMenu);
    {
        this->resetButton = Utils::createButton((char*)ANI_BUTTON_GRAY, (char*)ANI_BUTTON_GRAY, (char*)"Reset", 35, (char*)FONT_TRANSFORMER, Color3B(255,255,255));
        this->resetButton->setPosition(this->optionBoard->getContentSize().width / 2, 73);
        Face* lock = new Face();
        lock->initWithSpriteFrameName(ANI_ICO_LOCK);
        lock->setPosition(35, lock->getContentSize().height/2+5);
        this->resetButton->addChild(lock);
        resetMenu->addChild(this->resetButton);
    }

    
}

void OptionLayer::onCloseOptionBoard(Ref* sender) {
    this->notify(REQ_PAUSE, EVT_CLICK_BUTTON, NULL, 0, NULL);
}








